<?php
class Model_LeagueTeam extends Zend_Db_Table_Abstract
{
	protected $_name    = 'g1_league_team';
	protected $_primary = 'id';

    public function addTeam($league_id,$team_id,$team_name,$user_name,$logo_name){
    	
      $data['league_id']=$league_id;
      $data['team_id']=$team_id;
      $data['team_name']=$team_name;
      $data['logo']=$logo_name;
      $model_user = new Model_User();
      if(empty($user_name)){
      	$data['user_id'] = null;
      }else{
      	$model_user = new Model_User();
      	$data['user_id']=$model_user->getUserByName($user_name)->id;
      }
      

      $select = $this->select()
      			->from($this, 'count(id) as count')
      			->where('league_id = ?', $league_id)
      			->where('team_id = ?',$team_id)	;
      	
      $count = $this->fetchRow($select);
      
      $dispo = ($count->count == 0);
      if($dispo){
      	$this->insert($data);
      }else{
      	$this->update($data, 'league_id='.$league_id . ' and team_id = ' . $team_id);
      }
      
    }
    
    public function modifyTeam($league_id,$team_id,$team_name,$user_name,$logo_name){
    	
    	$data['team_name']=$team_name;
        $data['logo']=$logo_name;
    	if(empty($user_name)){
    		$data['user_id'] = null;
    	}else{
    		$model_user = new Model_User();
    		$data['user_id']=$model_user->getUserByName($user_name)->id;
    		
    	}
    	
    
    	$select = $this->select('user_id')
    
    	->where('league_id = ?', $league_id)
    	->where('team_id = ?',$team_id)	;
    	 
    	$old_user_id = $this->fetchRow($select)->user_id;
    	if(empty($old_user_id)){
    	   $model_game = new Model_Game();
    	   $model_game->addGamesByLeagueIdAndTeamId($league_id,$team_id);
    	}
    	

    	
    		$this->update($data, 'league_id='.$league_id . ' and team_id = ' . $team_id);
    	
    	
    	
    }
    
 
    
    public function getTeamsByLeagueId($league_id){
    	$select = " select u.username, lt.*
    			    from g1_league_team lt left join g1_user u on lt.user_id = u.id
    			    where lt.league_id = " . $league_id . " order by lt.team_id asc";
			
		return $this->_db->fetchAll( $select );
    }
    
    public function getFirstLeagueIdByUserId($user_id){
    	$select = $this->select('league_id')
    			->where('user_id=?', $user_id);
    	
    	$row = $this->fetchRow($select);
    	return $row;
    }
    
	public function getFirstActiveLeagueIdByUserId($user_id){
    	$select = " select lt.league_id
    			    from g1_league_team lt, g1_league l
    			    where status='new' and lt.league_id = l.id and lt.user_id = " . $user_id;
        $row = $this->_db->fetchRow($select);
    	return $row;
    }
    
    public function getFirstLeagueId(){
    	$select = $this->select('league_id');    	
    	 
    	$row = $this->fetchRow($select);
    	return $row;
    }
    
    public function getUsersByLeagueId($league_id){
    	$select = " select u.id, u.username
    			    from g1_league_team lt, g1_user u
    			    where 
    			          lt.user_id = u.id and lt.league_id = " . $league_id;
			
		return $this->_db->fetchAll( $select );
    }
    
    public function getLeaguesByUserId($user_id){
    	$select = " select lt.league_id, l.name
    			    from g1_league_team lt, g1_league l
    			    where lt.league_id = l.id and lt.user_id = " . $user_id;
			
		return $this->_db->fetchAll( $select );
    }
    
    public function getLeagues(){
    	$select = " select lt.league_id, l.name
    			    from g1_league_team lt, g1_league l
    			    where lt.league_id = l.id group by lt.league_id";
    		
    	return $this->_db->fetchAll( $select );
    }
    
    public function getActiveLeaguesByUserId($user_id){
    	$select = " select lt.league_id, l.name
    			    from g1_league_team lt, g1_league l
    			    where status='new' and lt.league_id = l.id and lt.user_id = " . $user_id;
    
    	return $this->_db->fetchAll( $select );
    }
	
	public function getLeagueTeamInfoByLeagueIdAndTeamId($league_id,$team_id){
	    	$select = $this->select()
			->where('league_id=?', $league_id)
    			->where('team_id=?', $team_id);
    	
    	$row = $this->fetchRow($select);
    	return $row;
	}
	
	public function deleteUserByLeagueIdAndTeamId($league_id,$team_id){

		$data['user_id'] = null;
		$this->update($data, 'league_id='.$league_id . ' and team_id = ' . $team_id);
		
		$model_game = new Model_Game();
		
		$model_game->deleteGamesByLeagueIdAndTeamId($league_id, $team_id);
	}
	   
	
}